<?php 
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\LinkPager;
?>
<!--用户管理模块-->
<div class="check-div form-inline">
	<div class="col-xs-3">
		<button class="btn btn-yellow btn-xs add" data-toggle="modal" data-target="#addUser">添加用户 </button>
	</div>
	<div class="col-xs-4">
		<input type="text" name='sou' value="<?php if(isset($_GET['sou']))echo $_GET['sou']?>" class="form-control input-sm" placeholder="输入用户名搜索">
		<button class="btn btn-white btn-xs submit">查 询 </button>
	</div>
<script>
$(function(){
	$('.submit').click(function(){
		var sou = $("input[name='sou']").val()
		location.href='?w=user/index&sou='+sou;
	});
})
</script>
</div>
<div class="data-div">
	<div class="row tableHeader">
		<div class="col-xs-1 ">
			用户名
		</div>
		<div class="col-xs-1">
			真实姓名
		</div>
		<div class="col-xs-2">
			电话
		</div>
		<div class="col-xs-2">
			角色
		</div>
		<div class="col-xs-2">
			注册时间
		</div>
		<div class="col-xs-2">
			上次登陆时间
		</div>
		<div class="col-xs-2">
			操作
		</div>
	</div>
	<div class="tablebody">
		<?php foreach ($data as $k => $row) {?>
		<div class="row show" show-id="<?=$row['id']?>">
			<div class="col-xs-1">
				<?=$row['name']?>
			</div>
			<div class="col-xs-1">
				<?=$row['realname']?>
			</div>
			<div class="col-xs-2">
				<?=$row['phone']?>
			</div>
			<div class="col-xs-2">
				<?php if(isset($role_name[$row['id']])) echo $role_name[$row['id']];?>
			</div>
			<div class="col-xs-2">
				<?php if($row['addtime']==''){?>
				
				<?php }else{?>
				<?=date('Y-m-d',$row['addtime'])?>
				<?php }?>
			</div>
			<div class="col-xs-2">
				<?php if($row['end_time']==''){?>
				从未登录
				<?php }else{?>
				<?=date('Y-m-d',$row['end_time'])?>
				<?php }?>
			</div>
			<div class="col-xs-2">
				<button u_id="<?=$row['id']?>" class="btn btn-success btn-xs update" data-toggle="modal" data-target="#reviseUser">修改</button>
				<button d_id="<?=$row['id']?>" class="btn btn-danger btn-xs delete" data-toggle="modal" data-target="#deleteUser">删除</button>
			</div>
		</div>
		<?php }?>
	</div>
	<script>
	$(function(){
		$('.delete').click(function(){
			$('#delete').attr('d_id',$(this).attr('d_id'));
		})
	})
	</script>
</div>
<!--页码块-->
	<footer class="footer">
		<ul class="pagination">
			<li>
				<select id="page">
					<?php for ($i=1; $i <=$pagination->pageCount ; $i++) { 
						if ($i == $page) {
						echo "<option selected>".$page."</option>";
					}else{?>

						<option><?=$i?></option>
					<?php }} ?>
				</select>
				页
			</li>
			<li class="gray">
				共<?=$pagination->pageCount?>页
			</li>
			<?=LinkPager::widget(['pagination' =>$pagination])?>
		</ul>
	</footer>
	<script>
		$(function(){
			$("#page").change(function(){
				var page = $(this).val()
				location.href='http://www.ly.com/Part-time%20job/backend/web/index.php?w=user%2Findex&page='+page;
			})
		})
	</script>

<!--弹出添加用户窗口-->
<div class="modal fade" id="addUser" role="dialog" aria-labelledby="gridSystemModalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="gridSystemModalLabel">添加用户</h4>
			</div>
			<div class="modal-body">
				<div class="container-fluid">
					<?php $form = ActiveForm::begin(['action'=>['user/add']]); ?>
		            <?= $form->field($model, 'name')->label('用户名');?>
		            <?= $form->field($model, 'pwd')->label('密码');?>
		            <?= $form->field($model, 'realname')->label('真实姓名');?>
		            <?= $form->field($model, 'phone')->label('电话');?>
		            <label class="control-label" for="adminrole-name">分配角色</label>
		            <div id='User[node_id]'>
			       		<?php foreach ($role as $k => $v) { ?>
			        	<label>
			        		<input name="User[role_id][]" type="checkbox" value='<?= $v['id']?>'>
			        		<?=$v['name']?>
			        	</label>
			        	<?php } ?>
					</div>
		            <?= $form->field($model, 'status')->radioList(['1'=>'正常','0'=>'禁用'])->label('状态');?> 
					
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn qu" data-dismiss="modal">取 消</button>
				<?= Html::submitButton('保存', ['class' => 'btn btn-primary']) ?>	
			</div> 
			<?php ActiveForm::end(); ?>
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<!--弹出修改用户窗口-->
<div class="modal fade" id="reviseUser" role="dialog" aria-labelledby="gridSystemModalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="gridSystemModalLabel">修改用户</h4>
			</div>
			<div class="modal-body">
				<div class="container-fluid">
					<form id="w1" action="/Part-time%20job/backend/web/index.php?w=user%2Findex" method="post">
					<input name="_csrfu" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">            
					<div class="form-group field-user-name required">
					<label class="control-label" for="user-name">用户名</label>
					<input type="text" id="user-name" class="form-control" name="name">

					<div class="help-block"></div>
					</div>		            
					<div class="form-group field-user-pwd required">
					<label class="control-label" for="user-pwd">密码</label>
					<input type="text" id="user-pwd" class="form-control" name="pwd">

					<div class="help-block"></div>
					</div>		            
					<div class="form-group field-user-realname required">
					<label class="control-label" for="user-realname">真实姓名</label>
					<input type="text" id="user-realname" class="form-control" name="realname">

					<div class="help-block"></div>
					</div>		            
					<div class="form-group field-user-phone required">
					<label class="control-label" for="user-phone">电话</label>
					<input type="text" id="user-phone" class="form-control" name="phone">
					<div class="help-block"></div>
					<label class="control-label" for="adminrole-name">分配角色</label>
		            <div id='node_id'>
			       		<?php foreach ($role as $k => $v) { ?>
			        	<label>
			        		<input name="role_id[]" type="checkbox" value='<?= $v['id']?>'>
			        		<?=$v['name']?>
			        	</label>
			        	<?php } ?>
					</div>
					<div class="help-block"></div>
					</div>		            
					<div class="form-group field-user-status">
					<label class="control-label" for="user-status">状态</label>
					<div id="user-status"><label><input type="radio" name="status" value="1"> 正常</label>
					<label><input type="radio" name="status" value="0"> 禁用</label></div>
					
					<div class="help-block"></div>
					</div>		            
		</form>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-xs btn-white" data-dismiss="modal">取 消</button>
				<button type="button" id='update' class="btn  btn-xs btn-green" data-dismiss="modal" u_id=''>修改</button>
			</div>
				
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<script>
	$(function(){
		$('.update').click(function(){
			$('input[type=text]').val('');
			// $('input[type=checkbox]').attr('checked',false);
			var id = $(this).attr('u_id')
			$('#update').attr('u_id',id);
			var status = $("input[name='status']");
			var role_id = $("input[name='role_id[]']");
			$.ajax({
				url:'?w=user/update',
				data:{id:id},
				dataType:'json',
				success:function(data){
					if(data.error){
						$("input[name='name']").val(data.msg.name)
						$("input[name='pwd']").val(data.msg.pwd)
						$("input[name='realname']").val(data.msg.realname)
						$("input[name='phone']").val(data.msg.phone)
						for(var i=0;i<status.size();i++)
						{
							if(status.eq(i).val()==data.msg.status)
							{
								status.eq(i).attr('checked','checked')
							}
						} 
						for(var i=0;i<role_id.size();i++)
						{
							if($.inArray(role_id.eq(i).val(),data.msg.role_id) != -1)
							{
								role_id.eq(i).attr('checked',true)
							}else{
								role_id.eq(i).attr('checked',false)
							}
						}
					}
				}
			})
		})
		$('#update').click(function(){
			var id = $(this).attr('u_id')
			var name = $("input[name='name']").val()
			var pwd = $("input[name='pwd']").val()
			var realname = $("input[name='realname']").val()
			var phone = $("input[name='phone']").val()
			var status = $("input[name='status']:checked").val()
			var role = $("input[name='role_id[]']");
			var role_id = [];
			for(var i=0;i<role.size();i++)
			{
				if(role.eq(i).prop('checked'))
				{
					role_id.push(role.eq(i).val())
				}
			}
			// alert(role_id)
			// return;
			var _csrfu = $("input[name='_csrfu']").val()
			$.ajax({
				type:'post',
				url:'?w=user/update',
				data:{
					_csrf:_csrfu,
					id:id,
					name:name,
					pwd:pwd,
					realname:realname,
					phone:phone,
					role_id:role_id,
					status:status
				},
				dataType:'json',
				success:function(data){
					if(!data.error){
						alert(data.msg)
					}else{
						alert(data.msg)
						location.href='?w=user/index';
					}
				}
			})
		})
	})
</script>
<!--弹出删除用户警告窗口-->
<div class="modal fade" id="deleteUser" role="dialog" aria-labelledby="gridSystemModalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="gridSystemModalLabel">提示</h4>
			</div>
			<div class="modal-body">
				<div class="container-fluid">
					确定要删除该用户！
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-xs btn-white" data-dismiss="modal">取 消</button>
				<button type="button" id='delete' class="btn  btn-xs btn-green" data-dismiss="modal" d_id=''>确 定</button>
			</div>
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<script>
	$(function(){
		$('#delete').click(function(){
			var id = $(this).attr('d_id');
			$.ajax({
				url:'?w=user/del',
				data:{id:id},
				dataType:'json',
				success:function(data){
					if(data.error){
						$('div[show-id='+id+']').remove();
					}else{
						alert('失败')
					}
				}
			})
		})
	})
</script>